<template>
  <div>
    <el-table :data="tableData"
              border
              style="width: 100%">
      <el-table-column prop="perm"
                       label="权限"
                       width="180">
      </el-table-column>
      <el-table-column prop="description"
                       label="描述">
      </el-table-column>
      <el-table-column label="操作"
                       width="180">
        <template slot-scope="scope">
          <el-button size="mini"
                     @click="updatePrem(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="deletPrem(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title"
               :visible.sync="editFormVisible"
               width="30%"
               @click="closeDialog">
      <el-form label-width="120px"
               :model="editForm"
               ref="editForm">
        <el-form-item label="权限"
                      prop="perm">
          <el-input size="small"
                    v-model="editForm.perm"
                    auto-complete="off"
                    readonly
                    placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="描述"
                      prop="description">
          <el-input size="small"
                    v-model="editForm.description"
                    auto-complete="off"
                    placeholder="请描述权限"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button size="small"
                   @click="closeDialog">取消</el-button>
        <el-button size="small"
                   type="primary"
                   class="title"
                   @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
    <!-- <el-button size="small"
               type="primary"
               icon="el-icon-plus"
               @click="updatePrem()">添加</el-button> -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        perm: "",
        description: "",
        flag: "",
        id: ""
      },
    }
  },
  created () {
    this.getdata()
  },
  inject: ["refresh"],
  methods: {
    getdata () {
      this.$axios.get('/api/perm/findAll').then(res => {
        if (res.data.code == 200) {
          console.log(res, '123123123');
          this.tableData = res.data.data
        }
      })
    },
    updatePrem: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.perm = row.perm
        this.editForm.description = row.description
        this.editForm.id = row.id
        this.editForm.flag = "revise"
      } else {
        this.title = '添加'
        this.editForm.perm = ''
        this.editForm.description = ''
        this.editForm.flag = 'add'
      }
    },
    submitForm (editData) {
      this.$refs[editData].validate(valid => {
        if (this.editForm.flag == "add") {
          this.$axios.post("/api/perm/add", this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.data.code == 200) {
                this.refresh()
              } else {

              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('系统维护......请稍后再试！')
            })
        }
        if (this.editForm.flag == "revise") {
          this.$axios.post("/api/perm/update", this.editForm)
            .then(res => {
              this.editFormVisible = false
              if (res.data.code == 200) {
                this.refresh()
              } else {
                alert("失败")
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('系统维护......请稍后再试！')
            })
        }

      })
    },
    deletPrem (index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.get("/api/perm/delet/" + row.id)
            .then(res => {
              if (res.data.code == 200) {
                this.refresh()
              } else {
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('系统维护中')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    closeDialog () {
      this.editFormVisible = false
    }
  }
}
</script>